﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonMethod
{
    class FilesDeal
    {
        //删除指定文件夹的文件
        public static void DeleteFiles(string srcPath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                        subdir.Delete(true);          //删除子目录和文件
                    }
                    else
                    {
                        File.Delete(i.FullName);      //删除指定文件
                    }
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }
        //获取指定硬盘大小
        public static float GetDriver(string Path)
        {

            float TotalSize = 0, FreeSize = 0;
            float gb = 1024 * 1024 * 1024;
            string s = "";

            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                //判断是否是固定磁盘
                if (drive.DriveType == DriveType.Fixed)
                {
                    if (drive.Name == Path)
                    {
                        TotalSize = drive.TotalSize / gb;
                        FreeSize = drive.TotalFreeSpace / gb;
                    }
                }
            }
            return (TotalSize - FreeSize) * 100 / TotalSize;//返回占用空间比例
        }
    }
}
